package com.campus.web.controller;
import com.campus.app.service.SocietyForsService;
import com.campus.commons.entity.OnlyIdentification;
import com.campus.commons.entity.Role;
import com.campus.commons.entity.SocietyFor;
import com.campus.commons.entity.User;
import com.campus.commons.utils.MyPage;
import com.campus.commons.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * <p>
 * 学社审核 前端控制器
 * </p>
 *
 * @author Rin
 * @since 2021-01-26
 */
@RestController
@RequestMapping("/society-for")
public class SocietyForController {

    @Autowired
    private SocietyForsService societyForsService;
    @PostMapping("/getArrg")
    public void Ge(Integer [] userId) {
        System.out.println("");
    }
    /**
     * 获取所有需要考试的同学
     * (这里会查询较多数据，存在性能问题，根据后期需求如果
     * 没太大改动，就再完善下数据的查询)
     * @param page
     * @param limit
     * @param Deid
     * @param roleId
     * @return
     */
    @RequestMapping("/getExams")
    public Result<List<SocietyFor>> getExams(int page, int limit,int Deid,int roleId) {
        Result<List<SocietyFor>> exams = societyForsService.getExams(page, limit, Deid, roleId);
        return exams;
    }
    @GetMapping("/")
    public ModelAndView toIndex(ModelMap modelMap,HttpSession session) {
        Role role= (Role) session.getAttribute("role");
        Integer state=0;
        switch (role.getRolename()) {
            case "班主任":
                state = 0;
                break;
            case "教员":
                state=1;
                break;
            case "部门老师":
                state = 3;
                break;
        }
        modelMap.addAttribute("state", state);
        return new ModelAndView("societyFor/tch/index",modelMap);
    }

    @GetMapping("/information")
    public ModelAndView toInformation(int id,ModelMap modelMap) {
        SocietyFor byId = societyForsService.getById(id);
        if (byId==null) {
            return new ModelAndView("error/405");
        }
        modelMap.addAttribute("info", byId);
        return new ModelAndView("societyFor/information",modelMap);
    }

     /**查询教员可以处理的列表
     * @param myPage
     * @return
     */

    @PostMapping("/getTch")
    public Result getTchCheck(@Valid MyPage myPage, HttpSession session) {
       OnlyIdentification onlyIdentification= (OnlyIdentification) session.getAttribute("identification");
        return societyForsService.getSocietyForsByTch_Q(myPage,onlyIdentification.getId());
    }


    /**
     * 查询班主任可以处理的列表
     * @param myPage
     * @return
     */
    @PostMapping("/getLifeTch")
    public Result getLifeTchCheck(@Valid MyPage myPage,HttpSession session) {
        OnlyIdentification onlyIdentification= (OnlyIdentification) session.getAttribute("identification");
        return societyForsService.getSocietyForsByLifeTch_Q(myPage,onlyIdentification.getId());
    }

    /**
     * 查询部门老师可以处理的列表
     * @param myPage
     * @return
     */
    @PostMapping("/getDeTch")
    public Result getDeTchCheck(@Valid MyPage myPage,HttpSession session) {
        User user = (User) session.getAttribute("user");
        OnlyIdentification onlyIdentification= (OnlyIdentification) session.getAttribute("identification");
        return societyForsService.getSocietyForsByDeTch_Q(myPage,user.getDeid(),onlyIdentification.getId());
    }


    /**
     *教员处理
     * @param id
     * @param opinion  评语
     * @param dispose  同意还是拒绝
     * @return
     */
    @PostMapping("/disposeByTch")
    public Result disposeTch(@NotNull@Min(1) Integer id, @NotBlank@Size(min = 1,max = 200) String opinion, boolean dispose) {
        SocietyFor societyFor = new SocietyFor(id);
        societyFor.setTchopinion(opinion);
        return societyForsService.disposeByTch_Q(societyFor, dispose);
    }

    /**
     * 班主任处理
     * @param id  id
     * @param opinion   评语
     * @param dispose  同意还是拒绝
     * @return
     */
    @PostMapping("/disposeByLifeTch")
    public Result disposeLifeTch(@NotNull@Min(1) Integer id, @NotBlank @Size(min = 1,max = 200) String opinion, boolean dispose) {
        SocietyFor societyFor = new SocietyFor(id);
        societyFor.setTchopinion(opinion);
        return societyForsService.disposeByLifeTch_Q(societyFor, dispose);
    }

    /**
     * 部门老师处理
     * @param id
     * @param opinion 评语
     * @param dispose  同意还是拒绝
     * @return
     */
    @PostMapping("/disposeByDeTch")
    public Result disposeDeTch(@NotNull@Min(1) Integer id, @NotBlank @Size(min = 1,max = 200) String opinion, boolean dispose) {
        SocietyFor societyFor = new SocietyFor(id);
        societyFor.setTchopinion(opinion);
        return societyForsService.disposeByDeTch_Q(societyFor, dispose);
    }

    /**
     * 发送多个邮箱的接口
     * @param studentId
     * @param text
     * @return
     */

    @PostMapping("/sendEmail")
    public Result sendEmail( Integer [] studentId,String text) {
        Result result = societyForsService.examsEmail(studentId, text);

        return result;
    }

    @PostMapping("/updateScore")
    public Result updateScore(int applyState,int computerExam,int interView,int penExam,int id,int studentId){
        Result result = societyForsService.updateScore(applyState, computerExam, interView, penExam, id, studentId);
        return result;
    }

    @PostMapping("/Scoresearch")
    public Result<List<SocietyFor>>Scoresearch(int deid,String studentName,int page,int limit ) {
        Result<List<SocietyFor>> scoresearch = societyForsService.Scoresearch(deid, studentName, page, limit);
        return scoresearch;
    }

    @PostMapping("/pass")
    public Result Pass(int id, int userId,int deId, int studentId) {
        Result result = societyForsService.passUser(id, userId, deId, studentId);

        return result;
    }
    @PostMapping("/no")
    public Result no(int id,  int studentId) {
        Result result = societyForsService.noPass(id, studentId);
        return result;
    }

}

